package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"real/models/mysql"
	"real/pkg"

	"real/realrpc/internal/svc"
	"real/realrpc/realrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RealNameLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRealNameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RealNameLogic {
	return &RealNameLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RealNameLogic) RealName(in *realrpc.RealNameRequest) (*realrpc.RealNameResponse, error) {
	// todo: add your logic here and delete this line

	pkg.RealName(in.IdCard, in.RealName)

	var users mysql.User

	mysql.DB.Debug().Where("id=?", in.Id).Find(&users)

	if users.ID != 0 {
		return nil, fmt.Errorf("用户已实名")
	}
	if in.IdCard < "18" {
		return nil, fmt.Errorf("用户未成年,不能使用支付功能")
	}
	user := mysql.User{
		Model: gorm.Model{ID: uint(in.Id)},

		IdCard:   in.IdCard,
		RealName: in.RealName,
	}
	mysql.DB.Debug().Updates(&user)
	return &realrpc.RealNameResponse{
		UserId: int64(user.ID),
	}, nil
}
